import { _decorator, Color, Component, instantiate, Label, Prefab, randomRangeInt, Size, Sprite } from 'cc';
import { DynamicScrollView } from "db://assets/scripts/prefab/DynamicScrollView/DynamicScrollView";
import { SingleColumnDynamicHeightTopToBottomLayout } from "db://assets/scripts/prefab/DynamicScrollView/layout/single_column_dynamic_height_layout/SingleColumnDynamicHeightTopToBottomLayout";
const { ccclass, property, executeInEditMode } = _decorator;

@ccclass('demo')
// @executeInEditMode(true)
export class demo extends Component {
    @property(DynamicScrollView)
    private dynamic_scorll_view: DynamicScrollView;
    @property(Prefab)
    private demo_rect: Prefab;

    protected start(): void {
        this._demo_test_1();
    }

    private _demo_test_1() {
        const layout = new SingleColumnDynamicHeightTopToBottomLayout("middle", new Size(50, 50), {
            spacingY: 20,
            anchor: { x: 0.5, y: 0.5 },
        });
        this.dynamic_scorll_view.init(layout, (item, view) => {
            return instantiate(this.demo_rect);
        }, {
            init_node: (node, item, view) => {
                node.getComponentInChildren(Label)!.string = `${item.index}`;
                const sprite = node.getComponent(Sprite);
                sprite.color = new Color(randomRangeInt(0, 256), randomRangeInt(0, 256), randomRangeInt(0, 256));
            }
        });

        //添加50个物体
        this.dynamic_scorll_view.layout.pushBackWithCount(50);
    }

    protected update(dt: number): void {
        this.node.getChildByPath("labels/1").getComponent(Label)!.string = `当前结点数:${this.dynamic_scorll_view.layout.getDisplayItemList().length}`;
        this.node.getChildByPath("labels/2").getComponent(Label)!.string = `当前显示的下标:${this.dynamic_scorll_view.layout.getDisplayItemList().map(item => item.index).join(",")}`;
        this.node.getChildByPath("labels/3").getComponent(Label)!.string = `总结点数:${this.dynamic_scorll_view.layout.getAllItem().length}`;
    }
}


